#!/usr/bin/env python
# -*- coding: utf-8 -*-

from dao.mongo_db import MongoDB
from datetime import datetime
import re

class ContentLabel(object):
    def __init__(self):
        self.mongo_scrapy = MongoDB(db='scrapy_data')
        self.mongo_recommendation = MongoDB(db='recommendation')
        self.scrapy_collection = self.mongo_scrapy.db_scrapy['content_ori']
        self.content_label_collection = self.mongo_recommendation.db_recommendation['content_label']

    def get_data_from_mongodb(self):
        datas = self.scrapy_collection.find()
        return datas

    def make_content_labels(self):
        datas = self.get_data_from_mongodb()
        for data in datas:
            #keywords = self.get_keywords(x.content, 10)
            word_nums = self.get_words_nums(data['desc'])
            content_collection = dict()
            content_collection['describe'] = data['desc']
            content_collection['word_nums'] = word_nums
            content_collection['type'] = data['type']
            content_collection['title'] = data['title']
            content_collection['news_date'] = data['times']
            content_collection['hot_heat'] = 10000
            content_collection['likes'] = 0
            content_collection['read'] = 0
            content_collection['collections'] = 0
            content_collection['create_time'] = datetime.utcnow()
            #content_collection['keywords'] = keywords
            #print(content_collection)
            self.content_label_collection.insert_one(content_collection)

    def get_words_nums(self, contents):
        ch = re.findall('([\u4e00-\u9fa5])', contents)
        num = len(ch)
        return num

if __name__ == '__main__':
    content = ContentLabel()
    content.make_content_labels()